package cn.ven.controller;

import cn.ven.entity.BookType;
import cn.ven.service.BookTypeService;
import cn.ven.tool.RespJson;
import cn.ven.tool.Token;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/api/adm/type")
public class BookTypeController {
    private static Logger log = LoggerFactory.getLogger(BookTypeController.class);

    @Autowired
    BookTypeService bookTypeService;

    //添加书本类型或修改类型
    @PostMapping("/addType")
    @ResponseBody
    public Map<String,Object> addType(Integer id,String type){
        if (Token.getUid()==null){ //uid==null，当用户没登陆的时候
            return RespJson.pack(401,"未登录，请先登录","");
        }
        BookType bookType=new BookType(); ///创建一个书本类型对象
        bookType.setType(type);
        bookType.setId(id);
        bookTypeService.addType(bookType);//添加类型或者修改
        List<BookType> bookTypes = bookTypeService.findAll();//查找所有的类型，用来返回给前端
        return RespJson.pack(200,"修改成功",bookTypes);
    }

    //查找所有的书本类型
    @GetMapping("/findAll")
    @ResponseBody
    public Map<String,Object> findAll(){
        List<BookType> bookTypes = bookTypeService.findAll();
        log.info("bookTypes==="+bookTypes);
        return RespJson.pack(200,"",bookTypes);
    }

    //根据id查找对应书本类型
    @GetMapping("/findById/{id}")
    @ResponseBody
    public Map<String,Object> findById(@PathVariable("id")Integer id){
        log.info("id=="+id);
        BookType bookType = bookTypeService.findById(id);
        return RespJson.pack(200,"",bookType);
    }

}
